package com.example.jchuah.tiles;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.media.Image;
import android.os.Bundle;
import android.os.Debug;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.text.AttributedCharacterIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Random;


public class MyActivity extends Activity implements View.OnTouchListener, View.OnDragListener {

    int numRows = 7;
    int numCols = 5;
    int timer = 0;

    View[][] grid = new View[numRows][numCols];
    GridLayout gridView;
    Random randGen = new Random();



    GestureDetector.SimpleOnGestureListener
            simpleOnGestureListener = new
            GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onDown(MotionEvent e) {
                    return true;
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (Math.abs(velocityX) > 3 ||
                            Math.abs(velocityY) > 3) {
                        if (Math.abs(velocityX) > Math.abs(velocityY)) {
                            if (velocityX > 0) {
                                flingRight();
                            } else {
                                flingLeft();
                            }
                        } else {
                            if (velocityY > 0) {
                                flingDown();

                            } else {
                                flingUp();
                            }
                        }
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            };
    GestureDetector gestureDetector;
    View lastTouched = null;











    public void flingLeft() {
        Log.d("fling", "left" + ((ImageView) lastTouched.findViewById(R.id.image)));
        RowColPair whereFrom = getRowColumn(lastTouched);
        RowColPair whereTo = whereFrom.getLeft();
        if (isValidRowColPair(whereTo)) {


            View sourceTile = extractTile(whereFrom);
            ImageView image1 = ((ImageView) sourceTile.findViewById(R.id.image));
            if (image1.getTag().equals("pause")){

                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_rew);
                image1.setTag("rewind");


            }
            if (image1.getTag().equals("rewind")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_rew);
                image1.setTag("rewind");
            }
            if (image1.getTag().equals("play")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_rew);
                image1.setTag("rewind");
            }
            if (image1.getTag().equals("fastforward")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_rew);
                image1.setTag("rewind");
            }



            addTile(whereTo, sourceTile);

        }

    }

    public void addTile(RowColPair pair, View tile) {
        addTile(pair.row, pair.col, tile);
    }

    public boolean isValidRowColPair(RowColPair pair) {
        return (pair.col >= 0 && pair.row >= 0 && pair.row < grid.length && pair.col < grid[0].length);
    }


    public void flingRight() {
        Log.d("fling", "right" + ((ImageView) lastTouched.findViewById(R.id.image)));
        RowColPair whereFrom = getRowColumn(lastTouched);
        RowColPair whereTo = whereFrom.getright();
        if (isValidRowColPair(whereTo)) {


            View sourceTile = extractTile(whereFrom);
            ImageView image1 = ((ImageView) sourceTile.findViewById(R.id.image));
            if (image1.getTag().equals("pause")) {

                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_ff);
                image1.setTag("fastforward");

            }
            if (image1.getTag().equals("rewind")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_ff);
                image1.setTag("fastforward");
            }
            if (image1.getTag().equals("play")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_ff);
                image1.setTag("fastforward");
            }
            if (image1.getTag().equals("fastforward")){
                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_ff);
                image1.setTag("fastforward");
            }

            addTile(whereTo,sourceTile);
            


        }
    }
    public View extractTile(RowColPair pair){
        View tile = grid[pair.row][pair.col];
        grid[pair.row][pair.col] = null;
        gridView.removeView(tile);
        return tile;
    }



    public void flingUp() {
        Log.d("fling", "up" + ((ImageView) lastTouched.findViewById(R.id.image)));
        RowColPair whereFrom = getRowColumn(lastTouched);
        RowColPair whereTo = whereFrom.getAbove();
        if (isValidRowColPair(whereTo)) {



            View sourceTile = extractTile(whereFrom);
            ImageView image1 = ((ImageView) sourceTile.findViewById(R.id.image));
            if (image1.getTag().equals("play")){

                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_play);
                image1.setTag("play");

            } else {
                if (image1.getTag().equals("rewind")) {
                    ((ImageView) sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_play);
                    image1.setTag("play");
                } else {
                    if (image1.getTag().equals("fastforward")) {
                        ((ImageView) sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_play);
                        image1.setTag("play");
                    }else{
                       if (image1.getTag().equals("pause")) {
                        ((ImageView) sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_play);
                        image1.setTag("play");
                    }
                }
            }




            addTile(whereTo, sourceTile);

        }

    }}

    public void flingDown() {
        Log.d("fling", "down" + ((ImageView) lastTouched.findViewById(R.id.image)));
        RowColPair whereFrom = getRowColumn(lastTouched);
        RowColPair whereTo = whereFrom.getBelow();
        if (isValidRowColPair(whereTo)) {


            View sourceTile = extractTile(whereFrom);
            ImageView image1 = ((ImageView) sourceTile.findViewById(R.id.image));


            if (image1.getTag().equals("pause")){


                ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_pause);
                image1.setTag("pause");


            }else{
               if (image1.getTag().equals("rewind")) {
                   ((ImageView) sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_pause);
                   image1.setTag("pause");
               }else
                  if (image1.getTag().equals("play")){
                    ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_pause);
                    image1.setTag("pause");
                }else
                   if (image1.getTag().equals("fastforward")){
                    ((ImageView)sourceTile.findViewById(R.id.image)).setImageResource(android.R.drawable.ic_media_pause);
                    image1.setTag("pause");
                }
            }



            addTile(whereTo, sourceTile);

        }

    }


    public void generateGrid() {
        View tile = getLayoutInflater()
                .inflate(R.layout.tile1, null);
        ImageView image1 = ((ImageView) tile.findViewById(R.id.image));
        image1.setImageResource(android.R.drawable.ic_media_pause);
        image1.setTag("pause");
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                View newTile = getLayoutInflater().inflate(R.layout.tile2,null);
                ImageView imageblank = ((ImageView) newTile.findViewById(R.id.blank));
                imageblank.setTag("blank");
                addTile(i, j, newTile);
            }
        }

        RowColPair target = new RowColPair();
        target.row = 0;
        target.col = 0;
        extractTile(target);

        addTile(0, 0, tile);
        View tile3 = getLayoutInflater()
                .inflate(R.layout.tile3, null);
        ImageView image3 = ((ImageView) tile3.findViewById(R.id.image3));
        image3.setImageResource(android.R.drawable.ic_media_play);
        image1.setTag("play");
        addTile(3,2,tile3);



        tile.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ImageView image = (ImageView)view.findViewById(R.id.image);
                RowColPair pair = getRowColumn(view);
                lastTouched = view;
                return
                        gestureDetector.onTouchEvent(motionEvent);


            }
        });


    }


    public void addTile(int row, int col, View tile) {
        GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
        layoutParams.rowSpec = GridLayout.spec(row);
        layoutParams.columnSpec = GridLayout.spec(col);
        tile.setLayoutParams(layoutParams);

        gridView.addView(tile);
        grid[row][col] = tile;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        gridView = (GridLayout) findViewById(R.id.gridView);
        generateGrid();
        gestureDetector = new GestureDetector
                (this, simpleOnGestureListener);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

    public RowColPair getRowColumn(View view) {
        RowColPair result = new RowColPair();
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == view) {
                    result.row = i;
                    result.col = j;
                    return result;
                }
            }
        }
        return result;
    }

    class RowColPair {
        int row = -1;
        int col = -1;
        public Object clone(){
            RowColPair theClone = new RowColPair();
            theClone.row = row;
            theClone.col = col;
            return theClone;
        }
        public RowColPair getBelow(){
            RowColPair result = new RowColPair();
            result.row = row + 1;
            result.col = col;
            return result;
        }
        public RowColPair getAbove(){
            RowColPair result = new RowColPair();
            result.row = row - 1;
            result.col = col;
            return result;
        }
        public RowColPair getLeft(){
            RowColPair result = new RowColPair();
            result.row = row;
            result.col = col - 1;
            return result;

        }
        public RowColPair getright(){
            RowColPair result = new RowColPair();
            result.row = row;
            result.col = col + 1;
            return result;
        }
    }
}
